<?php
/**
 * Created by PhpStorm.
 * User: huangshuaihui
 * Date: 2017/5/16
 * Time: 下午2:46
 */



namespace app\Http\Controllers\Admin;


use App\Http\Controllers\Admin\BaseController;
use App\Http\Model\Admin\Tourist;
use App\Http\Model\Admin\TouristAction;
use App\Http\Model\Admin\TouristInfo;
use Carbon\Carbon;
use Webpatser\Uuid\Uuid;
use Illuminate\Http\Request;
use Illuminate\Queue\RedisQueue;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Model\Admin\ExcelEtl;
use App\Http\Model\Admin\PushData;

class UploadController extends BaseController
{
    public function index(Request $request)
    {
        return view('admin.upload');
    }


    public function post(Request $request)
    {
        set_time_limit(60);
        $input = Input::all();
        $path = Input::file('file')->store('tourists/'.$input['source_id'].'/'.Carbon::now()->format('Ymd'));
        $real = realpath('../storage/app/'.$path);

        $PHPExcel = \PHPExcel_IOFactory::load($real);

        $currentSheet = $PHPExcel->getSheet(0);
        /**取得最大的列号*/
        $allColumn = $currentSheet->getHighestColumn();
        /**取得一共有多少行*/
        $allRow = $currentSheet->getHighestRow();
        $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($allColumn);

        $data = array();

        for ($row = 1;$row <= $allRow;$row ++) {
            $elem = array();

            for ($col = 0;$col < 23;$col++) {
                $val = $currentSheet->getCellByColumnAndRow($col,$row)->getValue();
                $elem[] = $val;
            }

            $data[] = $elem;
        }

        //var_dump($data);
//
        $etl = new ExcelEtl();

        foreach ($data as $da) {
            $result  = $etl->etl($da);
            if(false == $result){
                continue;
            }
            $tourist = new Tourist;
            $tourist->imei = $result['imei'];
            $tourist->idfa = $result['idfa'];
            $tourist->city = $result['city'];
            $tourist->source_id = $result['source_id'];
            $tourist->phone = $result['phone'];
            $tourist->save();
//            $tourist_rs = $tourist->save();
//            if($tourist_rs) {
//                $tourist_info = new TouristInfo;
//                $info = $tourist_info->findOneByPhone($result['phone']);
//
//                if (count($info) == 0 || !$info) {
//                    $tourist_info->phone = $result['phone'];
//                    $tourist_info->price = $result['house_price'];
//                    $tourist_info->area = $result['house_totalarea'];
//                    $tourist_info->cityarea_id = $result['cityarea_id'];
//                    $tourist_info->cityarea_name = $result['cityarea_name'];
//                    $tourist_info->house_room = $result['house_room'];
//                    $tourist_info->house_hall = $result['house_hall'];
//                    $tourist_info->house_toilet = $result['house_toilet'];
//                    $tourist_info->city = $result['city'];
//                    $tourist_info->appName = $result['appName'];
//                    $tourist_info->save();
//                }
//
//            }
            $action_ins = new TouristAction();
            $action_ins->setTable('tourists_actions' . $this->getTableNum($result['phone']));
            $result['id'] = Uuid::generate()->string;
            $result['user_id'] = $tourist['id'];
            $action_ins->createBatch($result);

        }
    }

}